import { Controller, Get,Post, Param, ParseIntPipe, Body, Delete,Request, Put } from '@nestjs/common';
import { UserService } from './user.service';
import { success } from 'src/utils';
@Controller('user')
export class UserController {
    constructor(private readonly userService: UserService) {}
    @Get('/wxUser/:id')
   async getUser(@Param('id',ParseIntPipe) id: number){
        try{
            const res = await this.userService.findOne(id)
            if(res){
                return res
            }else{
                return {code: 404,msg: '用户不存在'}
            }
        }catch(e){
            // console.log(e)
        }
        return 
    }
    @Post('wxUserList')
    userList(@Body() userInfo: any){
        return this.userService.findList(userInfo)
    }
    @Delete('delete/:id')
    deleteUser(@Param('id',ParseIntPipe) id: number){
        return this.userService.delete(id)
    }
    @Put('updataUser')
    async updataUser(@Body() data:any,@Request() req:any){
        const res = await this.userService.updateUser(data,req.user.id)
        return success(res,'修改成功');
    }
    @Get('addressList')
    async addressList(@Param() data:any,@Request() req:any){
        const res =  await this.userService.addressList({user_id: req.user.id})
        return success(res, '获取成功');
    }
    @Post('addAddress')
    async addAddress(@Body() data:any,@Request() req:any){
        const res =  await this.userService.addressCreate({...data,user_id: req.user.id})
        return success(res, data.id? '修改成功': '创建成功');
    }
    @Get('addressDetail/:id')
    async addressDetail(@Param('id',ParseIntPipe) id: number){
        const res =  await this.userService.addressDetail(id)
        return success(res, '获取成功');
    }
}
